
/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-12 09:49:56
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-12-15 10:21:22
 */
import gameCtr from "../ctrs/gameCtr";
import BaseGameComponent from "../libs/core/BaseGameComponent";
import { GameFacade } from "../libs/core/GameFacade";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { INotification } from "../libs/pureMvc/Interfaces";

const { ccclass, property } = cc._decorator;

@ccclass

export default class headItemCom extends BaseGameComponent {

  @property({ type: cc.Sprite, tooltip: "图标" })
  icon: cc.Sprite = null;

  @property({ type: cc.Node, tooltip: "是否解锁" })
  isLock: cc.Node = null;

  @property({ type: cc.Node, tooltip: "是否选中" })
  isSelect: cc.Node = null;

  private _data = null;

  public onEnable(): void {
    super.onEnable();
  }

  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_HEAD_UPDATE, PureMVCEvents.ON_ZBCOM_UDPATE];
  }

  public handleNotification(notification: INotification): void {
    let evtName = notification.getName();
    switch (evtName) {
      case PureMVCEvents.ON_HEAD_UPDATE:
        this.drawUi(null);
        break;
      case PureMVCEvents.ON_ZBCOM_UDPATE:
        this.drawUi(null);
        break;
    }
  }

  public drawUi(data) {
    if (data) {
      this._data = data;
    }

    let that = this;
    cc.resources.load(this._data.iconUrl, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
      if (err) return;
      that.icon.spriteFrame = sp;
    })

    this.isLock.active = this._data.isLock;

    if (Number(gameCtr.ins.avatar) == this._data.id) {
      this.isSelect.active = true;
    } else {
      this.isSelect.active = false;
    }
  }

  onClick(e) {
    // console.log()
    if (this._data.id == Number(gameCtr.ins.avatar)) return;
    if (this._data.isLock) return;
    gameCtr.ins.avatar = this._data.id;
    GameFacade.getInstance().sendNotification(PureMVCEvents.ON_HEAD_UPDATE);
    GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
  }
}